module.exports = {
    /**@param {Creep}creep */
    run: function (creep) {
        var sourcepos = {
            E1S31: {
                x: 21,
                y: 20,
            },
            E3S32: {
                x: 26,
                y: 20,
            }
        };

        var freepos = {
            E1S31: {
                x: 22,
                y: 11,
            },
            E3S32: {
                x: 26,
                y: 24,
            }
        };

        if (creep.memory.supply) {
            if (creep.store[RESOURCE_ENERGY] < 5) {
                creep.memory.supply = false;
            }
            else {
                var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
                    filter: structure => (structure.structureType == STRUCTURE_SPAWN ||
                        structure.structureType == STRUCTURE_EXTENSION) &&
                        structure.store.getFreeCapacity(RESOURCE_ENERGY) > 5
                });
                if (target) {
                    creep.say('s');
                    if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(target);
                    }
                }
                else {
                    creep.say('f');
                    creep.moveTo(freepos[creep.room.name].x,
                        freepos[creep.room.name].y);
                }
            }
        }
        else {
            if (creep.store.getFreeCapacity() < 5) {
                creep.memory.supply = true;
            }
            else {
                var target = creep.room.getPositionAt(sourcepos[creep.room.name].x,
                    sourcepos[creep.room.name].y).findClosestByRange(FIND_DROPPED_RESOURCES, {
                        filter: resource => resource.resourceType == RESOURCE_ENERGY &&
                            resource.amount > 10
                    });
                if (target) {
                    creep.say('p');
                    if (creep.pickup(target) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(target);
                    }
                }
                else {
                    target = creep.pos.findClosestByRange(FIND_TOMBSTONES, {
                        filter: structure => structure.store[RESOURCE_ENERGY] > 10
                    });
                    if (target) {
                        creep.say('c');
                        if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                            creep.moveTo(target);
                        }
                    }
                    else {
                        target = creep.room.getPositionAt(sourcepos[creep.room.name].x,
                            sourcepos[creep.room.name].y).findClosestByRange(FIND_STRUCTURES, {
                                filter: structure => structure.structureType == STRUCTURE_CONTAINER &&
                                    structure.store[RESOURCE_ENERGY] > 50
                            });
                        if (target) {
                            creep.say('g');
                            if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                                creep.moveTo(target);
                            }
                        }
                        else {
                            if (creep.store[RESOURCE_ENERGY] > 50) {
                                target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
                                    filter: structure => (structure.structureType == STRUCTURE_SPAWN ||
                                        structure.structureType == STRUCTURE_EXTENSION) &&
                                        structure.store.getFreeCapacity(RESOURCE_ENERGY) > 5
                                });
                                if (target) {
                                    creep.say('s');
                                    if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                                        creep.moveTo(target);
                                    }
                                }
                                else {
                                    creep.say('f');
                                    creep.moveTo(freepos[creep.room.name].x,
                                        freepos[creep.room.name].y);
                                }
                            }
                            else {
                                creep.say('f');
                                creep.moveTo(freepos[creep.room.name].x,
                                    freepos[creep.room.name].y);
                            }
                        }
                    }

                }
            }
        }
    }
}